package com.chen.authority.controller;

import com.chen.authority.common.annotation.Log;
import com.chen.authority.common.enums.BusinessType;
import com.chen.authority.common.response.PageResponse;
import com.chen.authority.common.response.Response;
import com.chen.authority.common.response.SingleResponse;
import com.chen.authority.model.dto.NoticeDto;
import com.chen.authority.model.qo.NoticeQo;
import com.chen.authority.common.model.PageQo;
import com.chen.authority.model.vo.NoticeVo;
import com.chen.authority.service.NoticeService;
import lombok.RequiredArgsConstructor;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.validation.Valid;
import java.util.Arrays;

/**
 * 通知公告管理
 *
 * @author chengy
 * @date 2022/11/19
 */
@Api(tags = "通知公告管理 NoticeController")
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/notice")
public class NoticeController {

    private final NoticeService noticeService;

    @PostMapping
    @ApiOperation("新增通知公告")
    @SaCheckPermission("system:notice:add")
    @Log(module = "系统管理-通知公告", businessType = BusinessType.INSERT)
    public Response add(@Valid @RequestBody NoticeDto dto) {
        return noticeService.addOrUpdateNotice(null, dto);
    }

    @DeleteMapping("/{ids}")
    @ApiOperation("删除通知公告")
    @SaCheckPermission("system:notice:delete")
    @Log(module = "系统管理-通知公告", businessType = BusinessType.DELETE)
    public Response delete(@PathVariable Integer[] ids) {
        return noticeService.removeByIds(Arrays.asList(ids)) ? Response.success() : Response.fail();
    }

    @PutMapping("/{id}")
    @ApiOperation("更新通知公告")
    @SaCheckPermission("system:notice:update")
    @Log(module = "系统管理-通知公告", businessType = BusinessType.UPDATE)
    public Response update(@PathVariable Integer id, @Valid @RequestBody NoticeDto dto) {
        return noticeService.addOrUpdateNotice(id, dto);
    }

    @GetMapping("/{id}")
    @ApiOperation("获取通知公告详情")
    @SaCheckPermission("system:notice:update")
    public SingleResponse<NoticeVo> get(@PathVariable Integer id) {
        return SingleResponse.success(noticeService.getById(id).toVo(NoticeVo.class));
    }

    @GetMapping("/list")
    @ApiOperation("查询通知公告列表")
    @SaCheckPermission("system:notice:list")
    public PageResponse<NoticeVo> list(PageQo pageQo, NoticeQo noticeQo) {
        return PageResponse.success(noticeService.pageQueryNoticeList(pageQo, noticeQo));
    }

}